<?php

session_start();

header("Content-Type:application/vnd.adobe.pdf");

$fullname = $_SESSION["fullname"];
require_once('./fpdf/tcpdf/tcpdf.php');

//require_once('./fpdf/FPDI/fpdi.php');

class PDF extends TCPDF {

    function setVar($fullname) {
        $this->fullname = $fullname;
    }

    function Header() {
        $this->SetFont("thsarabun", "B", 14);
        $header = '
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td height="33" colspan="5">เทศบาลนครอุดรธานี (ผู้ปฏิบัติงาน : ' . $this->fullname . ')</td>
                  <td colspan="4" align="right">วันที่ ' . date('d / m / Y') . '</td>
                </tr>
            </table>
            <table width="100%" border="1" cellspacing="0" cellpadding="0">
                <tr align="center" nobr="true" valign="middle" >
                    <td width="10%">วันที่</td>
                    <td width="20%">ชื่อผู้มีสิทธิ์</td>
                    <td width="10%">เช็คเลขที่</td>
                    <td width="15%">ธนาคาร</td>
                    <td width="5%">รหัสแผนงาน</td>
                    <td width="5%">รหัสงาน</td>
                    <td width="15%">หมวดรายจ่าย</td>
                    <td width="7%">จำนวนเงิน</td>
                    <td width="6%">ภาษีหัก ณ ที่จ่าย</td>
                    <td width="7%">จ่ายสุทธิ</td>
                </tr>
            </table>';
        $this->writeHTML('<br><div align="center">รายงานสมุดเงินสดจ่าย</div>' . $header, true, false, false, false, '');
    }

}

//$pdf = new TCPDF($orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);
$pdf = new PDF($orientation = 'L', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);

$pdf->setVar($fullname);
$pdf->SetMargins(10, 38, 10);
$pdf->SetAutoPageBreak(true, 20);
$pdf->AddPage();

//$pdf->SetFont("thsarabunb", "", 14);
$pdf->SetFont('thsarabun', '', 14);

require_once("includes/connect.php");
require_once("includes/function.php");

$db = new myDBS();
$db->selectDB("account");

//$sql = "SELECT DISTINCT date_create,  "
//        . " FROM tbl_pet, bank, bank_branch,tbl_pay "
//        . " WHERE tbl_pet.payment_method in( 'เช็ค', 'โอนเงิน', 'เงินสด') "
//        . " AND tbl_pet.pet_id = tbl_pay.pet_id "
//        . " AND tbl_pay.bank_id = bank.bank_id "
//        . " AND tbl_pay.bank_branch_id = bank_branch.bank_branch_id ";
$sql = "SELECT DISTINCT date_create, credit_name AS receiver, tbl_pay.cheque_no, bank.bank_name  "
                . " , panngan2.panngan2_id, panngan3.panngan3_id, ex2_id, expenses2_name, ex3_id, tbl_pay.amount, tt1, tbl_pet.net "
                . " , tbl_pet.pet_id "
                . " FROM tbl_creditor, bank, bank_branch,tbl_pay, panngan2, panngan3, tbl_pet"
                . " LEFT JOIN expenses_lvl2 ON ex2_id = expenses_lvl2.expenses2_id "
                . " LEFT JOIN expenses_lvl3 ON ex3_id = expenses_lvl3.expenses3_id "
                . " WHERE tbl_pet.payment_method in( 'เช็ค', 'โอนเงิน', 'เงินสด') "
                . " AND tbl_pet.receiver = tbl_creditor.creditor_id "
                . " AND panngan2.panngan2_id = tbl_pet.panngan2_id "
                . " AND panngan3.panngan3_id = tbl_pet.panngan3_id "
                . " AND tbl_pet.pet_id = tbl_pay.pet_id "
                . " AND tbl_pay.bank_id = bank.bank_id ";
//                . " AND tbl_pay.bank_branch_id = bank_branch.bank_branch_id "
//                . " ORDER BY tbl_pay.cheque_no";

//echo $sql;
$result = $db->runSQL($sql);

$tbl_header = '<table cellspacing="0" cellpadding="0" border="0" width="100%">';
//    $tbl_body = $tbl_body . '
//        <tr>
//            <td align="center">' . $sql . '</td>
//        </tr>
//    ';

while ($rows = $db->fetch_object($result)) {
    
    $tbl_body = $tbl_body . '
                    <tr nobr="true">
                    <td width="10%" align="center">' . $rows->date_create . '</td>
                    <td width="20%" align="left">' . $rows->receiver . '</td>
                    <td width="10%" align="center">' . $rows->cheque_no . '</td>
                    <td width="15%" align="center">' . $rows->bank_name . '</td>
                    <td width="5%" align="center">' . $rows->panngan2_id . '</td>
                    <td width="5%" align="center">' . $rows->panngan3_id . '</td>
                    <td width="15%" align="center">' . $rows->expenses2_name . '</td>
                    <td width="7%" align="right" border="0">' . number_format($rows->amount, 2) . '</td>
                    <td width="5%" align="right" border="0">' . number_format($rows->tt, 2) . '</td>
                    <td width="7%" align="right" border="0">' . number_format($rows->net, 2) . '</td>
                    </tr>

                    ';

}

$tbl_footer = '</table><hr>';

$pdf->writeHTML($tbl_header . $tbl_body . $tbl_footer, true, false, false, false, '');
//$pdf->write($sql);

$pdf->Output("pdf", 'I');
?>